﻿/*
 *	交易页点击事件
 */

$(document).ready(function () {
    utc_click_addtrade();
    utc_click_altertrade();
    utc_click_deletetrade();
    utc_click_recovertrade();
});

/// 添加交易
function utc_click_addtrade() {
    $(".trade_add_a").click(function (e) {
        var thishandle = $(this).parent().parent().parent().parent();

        var trade_time = thishandle.find(".rt_tradetime").val();
        var trade_refrences_product_name = thishandle.find(".rt_productname").val();
        var trade_sum = thishandle.find(".rt_tradesum").val();
        var trade_price = thishandle.find(".rt_tradeprice").val();
        var trade_amount = thishandle.find(".rt_tradeamount").val();
        var trade_customer = thishandle.find(".rt_tradecustom").val();
        var trade_decript = thishandle.find(".rt_tradedescript").val();

        if (trade_time == "") {
            thishandle.find(".rt_tradetime").focus();
            return;
        }
        if (trade_refrences_product_name == "") {
            thishandle.find(".rt_productname").focus();
            return;
        }
        if (trade_sum == "") {
            thishandle.find(".rt_tradesum").focus();
            return;
        }
        if (trade_price == "") {
            thishandle.find(".rt_tradeprice").focus();
            return;
        }
        if (trade_amount == "") {
            thishandle.find(".rt_tradeamount").focus();
            return;
        }
        if (trade_customer == "") {
            thishandle.find(".rt_tradecustom").focus();
            return;
        }
        if (trade_decript == "") {
            thishandle.find(".rt_tradedescript").focus();
            return;
        }
        if (trade_time == "") {
            thishandle.find(".rt_tradetime").focus();
            return;
        }

        var email = $("#ru_g_id").val();
        email = $.trim(email);

        $.ajax({
            url: "/Mange_Main/AddTrade",
            type: "post",
            cache: false,
            data: {
                trade_customer: trade_customer,
                trade_refrences_product_name: trade_refrences_product_name,
                trade_sum: trade_sum,
                trade_price: trade_price,
                trade_amount: trade_amount,
                trade_decript: trade_decript,
                trade_time: trade_time,
                trade_refrences_user_email: email
            },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    var trade_new = $("<!-- 交易列表item div-->"
                                + "<div class='rtr_tradelistitembox_div' ><div class='rtr_tradelistitem_div' >"
                                + "    <ul>"
                                + "        <li class='rtr_row_tradeid'><input type='text' class='rt_tradeid' maxlength='50' placeholder='ID' value='" + msg.tradeid + "' readonly='true' disable='true'/></li>"
                                + "        <li class='rtr_row_tradetime'>"
                                + "            <input type='text' class='rt_tradetime' maxlength='50' placeholder='时间' value='" + trade_time + "' /></li>"
                                + "        <!-- 产品名 -->"
                                + "        <li class='rtr_row_tradename'>"
                                + "            <input type='text' class='rt_productname' maxlength='50' placeholder='产品名' value='" + trade_refrences_product_name + "' />"
                                + "            <a class='rt_productname_select'></a></li>"
                                + "        <!-- /产品名 -->"
                                + "        <li class='rtr_row_tradesum'>"
                                + "            <input type='text' class='rt_tradesum' maxlength='50' placeholder='数量(/件)' value='" + trade_sum + "' /></li>"
                                + "        <li class='rtr_row_tradeprice'>"
                                + "            <input type='text' class='rt_tradeprice' maxlength='50' placeholder='单价(/元)' value='" + trade_price + "' /></li>"
                                + "        <li class='rtr_row_tradeaccount'>"
                                + "            <input type='text' class='rt_tradeamount' maxlength='50' placeholder='总额(/元)' value='" + trade_amount + "' /></li>"
                                + "        <li class='rtr_row_tradecustom'>"
                                + "            <input type='text' class='rt_tradecustom' maxlength='50' placeholder='客户' value='" + trade_customer + "' /></li>"
                                + "        <div class='rtr_row_tradedescript'>"
                                + "            <input type='text' class='rt_tradedescript' maxlength='50' placeholder='备注' value='" + trade_decript + "' /></div>"
                                + "    </ul>"
                                + "</div></div>"
                                + "<!-- /交易列表item div -->");
                    $(".rtr_tradelist_div").prepend(trade_new);
                }
                else {
                alert(msg.Message);
                }

            }
        });

    });
}

/// 修改交易
function utc_click_altertrade() {
    $(".trade_alter_a").live("click", function (e) {
        var thishandle = $(this).parent().parent().parent().parent();

        var trade_id = thishandle.parent().parent().find(".rt_tradeid").val();
        if (trade_id == "") {
            alert("修改失败");
            return;
        }


        var trade_time = thishandle.parent().parent().find(".rt_tradetime").val();
        var trade_refrences_product_name = thishandle.parent().parent().find(".rt_productname").val();
        var trade_sum = thishandle.parent().parent().find(".rt_tradesum").val();
        var trade_price = thishandle.parent().parent().find(".rt_tradeprice").val();
        var trade_amount = thishandle.parent().parent().find(".rt_tradeamount").val();
        var trade_customer = thishandle.parent().parent().find(".rt_tradecustom").val();
        var trade_decript = thishandle.parent().parent().find(".rt_tradedescript").val();

        if (trade_time == "") {
            thishandle.find(".rt_tradetime").focus();
            return;
        }
        if (trade_refrences_product_name == "") {
            thishandle.find(".rt_productname").focus();
            return;
        }
        if (trade_sum == "") {
            thishandle.find(".rt_tradesum").focus();
            return;
        }
        if (trade_price == "") {
            thishandle.find(".rt_tradeprice").focus();
            return;
        }
        if (trade_amount == "") {
            thishandle.find(".rt_tradeamount").focus();
            return;
        }
        if (trade_customer == "") {
            thishandle.find(".rt_tradecustom").focus();
            return;
        }
        if (trade_decript == "") {
            thishandle.find(".rt_tradedescript").focus();
            return;
        }
        if (trade_time == "") {
            thishandle.find(".rt_tradetime").focus();
            return;
        }



        var truthBeTold = window.confirm("确定修改?");
        if (truthBeTold) {
            $.ajax({
                url: "/Mange_Main/AlterTrade",
                type: "post",
                cache: false,
                data: {
                    trade_id: trade_id,
                    trade_customer: trade_customer,
                    trade_refrences_product_name: trade_refrences_product_name,
                    trade_sum: trade_sum,
                    trade_price: trade_price,
                    trade_amount: trade_amount,
                    trade_decript: trade_decript,
                    trade_time: trade_time
                },
                dataType: "json",
                success: function (msg) {
                    if (msg.Result) {
                        alert(msg.Message);
                    }
                    else {
                        alert(msg.Message);
                    }
                }
            });
        }
    });
}

/// 删除交易
function utc_click_deletetrade() {
    $(".trade_delete_a").live("click", function (e) {
        var thishandle = $(this).parent().parent().parent().parent();

        var trade_id = thishandle.parent().parent().find(".rt_tradeid").val();
        if (trade_id == "") {
            alert("删除失败");
            return;
        }
        var truthBeTold = window.confirm("确定删除?");
        if (truthBeTold) {
            $.ajax({
                url: "/Mange_Main/DeleteTrade",
                type: "post",
                cache: false,
                data: {
                    trade_id: trade_id
                },
                dataType: "json",
                success: function (msg) {
                    if (msg.Result) {
                        thishandle.parent().parent().remove();
                    }
                    else {
                        alert(msg.Message);
                    }
                }
            });
        }
    });
}

/// 恢复交易
function utc_click_recovertrade() {
    $(".trade_recover_a").live("click", function (e) {
        var thishandle = $(this).parent().parent().parent().parent();

        var trade_id = thishandle.parent().parent().find(".rt_tradeid").val();
        if (trade_id == "") {
            alert("恢复失败");
            return;
        }

            $.ajax({
                url: "/Mange_Main/GetRemoverTrade",
                type: "post",
                cache: false,
                data: {
                    trade_id: trade_id
                },
                dataType: "json",
                success: function (msg) {
                    if (msg.Result) {
                        thishandle.parent().parent().find(".rt_tradetime").val(msg.trade_time);
                        thishandle.parent().parent().find(".rt_productname").val(msg.trade_refrences_product_name);
                        thishandle.parent().parent().find(".rt_tradesum").val(msg.trade_sum);
                        thishandle.parent().parent().find(".rt_tradeprice").val(msg.trade_price);
                        thishandle.parent().parent().find(".rt_tradeamount").val(msg.trade_amount);
                        thishandle.parent().parent().find(".rt_tradecustom").val(msg.trade_customer);
                        thishandle.parent().parent().find(".rt_tradedescript").val(msg.trade_decript);
                    }
                    else {
                        alert(msg.Message);
                    }
                }
            });
      
    });
}